package com.example.project.controller;

import cn.afterturn.easypoi.entity.vo.NormalExcelConstants;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.enmus.ExcelType;
import cn.afterturn.easypoi.view.PoiBaseView;
import com.example.project.annotations.PassToken;
import com.example.project.common.Result;
import com.example.project.entity.TableC;
import com.example.project.service.TableCService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * Created by Ethan on 2022-08-26 17:55
 */
@RestController
@Slf4j
@RequestMapping("/tableC")
public class TableCController {
    @Autowired
    private TableCService tableCService;

    /**
     * 计算
     * @param s
     * @return
     */
    @PassToken
    @GetMapping("/compute")
    public Result<List<TableC>> compute(String s) {
        tableCService.computeAndSave(s);
        List<TableC> tableCS = tableCService.findByName(s);
        return Result.success(tableCS);
    }

    @PassToken
    @GetMapping("/out")
    public void out(ModelMap map,
                    HttpServletRequest request,
                    HttpServletResponse response) {
        // 从数据库获取数据
        List<TableC> tableCS = tableCService.findAll();
        // 设置输出参数
        ExportParams params = new ExportParams("C表", "sheet1", ExcelType.XSSF);
        // 输出日志
        for (TableC c : tableCS) {
            log.info(c.toString());
        }
        map.put(NormalExcelConstants.DATA_LIST, tableCS);
        map.put(NormalExcelConstants.CLASS, TableC.class);
        map.put(NormalExcelConstants.PARAMS, params);
        map.put(NormalExcelConstants.FILE_NAME, "tableCTest");
        PoiBaseView.render(map, request, response, NormalExcelConstants.EASYPOI_EXCEL_VIEW);
    }
}
